package vip.xiaonuo.invoice.modular.vo;

import com.alibaba.fastjson.annotation.JSONField;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import vip.xiaonuo.invoice.core.adapter.dto.result.RedInvoiceResultDtoOfShuZu;
import vip.xiaonuo.invoice.core.enums.invoice.InvoicePlatformEnum;

import java.math.BigDecimal;
import java.util.Objects;

/**
 * 统一发票详情VO
 * 用于统一不同平台的发票查询结果输出格式
 *
 * @author AI Assistant
 */
@Data
public class RedInvoiceDetailVo {
    /**
     * 红票跟踪ID
     */
    @JSONField(ordinal = 1)
    @Schema(description = "红票跟踪ID")
    private String redInvoiceTraceId;

    /**
     * 原发票类型代码
     */
    @JSONField(ordinal = 4)
    @Schema(description = "原发票类型代码")
    private String originalInvoiceTypeCode;

    /**
     * 原发票号码
     */
    @JSONField(ordinal = 5)
    @Schema(description = "原发票号码")
    private String originalInvoiceNo;


    /**
     * 冲红类型
     */
    @JSONField(ordinal = 7)
    @Schema(description = "冲红类型 03全额红冲 04部分红冲")
    private String redType;

    /**
     * 冲红原因代码
     */
    @JSONField(ordinal = 8)
    @Schema(description = "冲红原因代码")
    private String redReasonCode;

    /**
     * 申请来源
     */
    @JSONField(ordinal = 9)
    @Schema(description = "申请来源：0-销方申请 1-购方申请")
    private String applicationSource;

    /**
     * 开票平台编码
     */
    @JSONField(ordinal = 9)
    @Schema(description = "开票平台")
    private String platform;

    /**
     * 购方名称
     */
    @JSONField(ordinal = 10)
    @Schema(description = "购方名称")
    private String buyerName;

    /**
     * 购方税号
     */
    @JSONField(ordinal = 11)
    @Schema(description = "购方税号")
    private String buyerTaxNumber;

    /**
     * 销方名称
     */
    @JSONField(ordinal = 12)
    @Schema(description = "销方名称")
    private String sellerName;

    /**
     * 销方税号
     */
    @JSONField(ordinal = 13)
    @Schema(description = "销方税号")
    private String sellerTaxNumber;

    /**
     * 开票人
     */
    @JSONField(ordinal = 14)
    @Schema(description = "开票人")
    private String issuer;

    /**
     * 红票状态码
     */
    @JSONField(ordinal = 15)
    @Schema(description = "红票状态")
    private String redInvoiceStatus;


    @JSONField(ordinal = 15)
    @Schema(description = "描述")
    private String description;

    /**
     * 红票日期
     */
    @JSONField(ordinal = 16)
    @Schema(description = "冲红日期")
    private String redInvoiceDate;

    /**
     * 原开票日期
     */
    @JSONField(ordinal = 6)
    @Schema(description = "原开票日期")
    private String originalInvoiceDate;


    /**
     * 冲红金额(不含税)
     */
    @JSONField(ordinal = 17)
    @Schema(description = "冲红金额(不含税)")
    private BigDecimal redAmount;

    /**
     * 冲红税额
     */
    @JSONField(ordinal = 18)
    @Schema(description = "冲红税额")
    private BigDecimal redTax;

    /**
     * 冲红价税合计
     */
    @JSONField(ordinal = 19)
    @Schema(description = "冲红价税合计")
    private BigDecimal totalRedAmount;

    /**
     * PDF文件URL
     */
    @JSONField(ordinal = 20)
    @Schema(description = "PDF文件URL")
    private String pdfUrl;

    /**
     * OFD文件URL
     */
    @JSONField(ordinal = 21)
    @Schema(description = "OFD文件URL")
    private String ofdUrl;

    /**
     * XML文件URL
     */
    @JSONField(ordinal = 22)
    @Schema(description = "XML文件URL")
    private String xmlUrl;

    /**
     * 错误信息
     */
    @JSONField(ordinal = 23)
    @Schema(description = "错误信息")
    private String errorMessage;

    /**
     * 商品明细列表
     */
    @JSONField(ordinal = 24)
    @Schema(description = "商品明细列表")
    private String itemList ;


    /**
     * 从数族红票结果DTO转换为统一VO
     */
    public static RedInvoiceDetailVo fromShuZuDto(RedInvoiceResultDtoOfShuZu dto) {
        RedInvoiceDetailVo vo = new RedInvoiceDetailVo();

        // 设置红票详情
        if (dto.getContent() != null) {
            RedInvoiceResultDtoOfShuZu.content shuZuContent = dto.getContent();

            // 设置购方信息
            vo.setBuyerName(shuZuContent.getGfmc());
            vo.setBuyerTaxNumber(shuZuContent.getGfsh());

            // 设置销方信息
            vo.setSellerName(shuZuContent.getXfmc());
            vo.setSellerTaxNumber(shuZuContent.getXfsh());

            // 设置基本信息
            vo.setIssuer(shuZuContent.getKpr());
            vo.setRedInvoiceDate(shuZuContent.getKprq());
            vo.setPdfUrl(shuZuContent.getPdfUrl());

            // 设置原发票信息
            vo.setOriginalInvoiceNo(shuZuContent.getFphm());

            // 转换金额字段为BigDecimal
            if (shuZuContent.getHjje() != null) {
                vo.setRedAmount(new BigDecimal(shuZuContent.getHjje()));
            }
            if (shuZuContent.getHjse() != null) {
                vo.setRedTax(new BigDecimal(shuZuContent.getHjse()));
            }
            if (shuZuContent.getJshj() != null) {
                vo.setTotalRedAmount(new BigDecimal(shuZuContent.getJshj()));
            }

            // 直接将商品明细设置为JSON字符串
            if (shuZuContent.getMxList() != null) {
                vo.setItemList(com.alibaba.fastjson.JSON.toJSONString(shuZuContent.getMxList()));
            }
        }
        return vo;
    }

    /**
     * 创建基础的红票详情VO
     */
    public static RedInvoiceDetailVo createBaseVo(String redInvoiceTraceId, String redInvoiceStatus) {
        RedInvoiceDetailVo vo = new RedInvoiceDetailVo();
        vo.setRedInvoiceTraceId(redInvoiceTraceId);
        // 默认使用SHUZU平台代码
        vo.setPlatform(Objects.requireNonNull(InvoicePlatformEnum.getByCode("SHUZU")).getCode());
        vo.setRedInvoiceStatus(redInvoiceStatus);
        return vo;
    }
}
